<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@include file="/include/common/gridbackhead.jsp"%>
<%!
private class PageAction extends BaseAction{
	ResultSet rs = null;
	//加載頁面默認數據
	protected boolean onLoadDefault() throws Exception{
		form.spsv("psysName", "fsPro");
		onSearchRetrieveResult();
		return true ;
	}
	
	protected void EndProcess() throws Exception{
		if("save".equals(form.getAction())){
			onSearchRetrieveResult();
		}
	}
	
	// 取出查询結果
	protected void onSearchRetrieveResult() throws Exception{
		ResultSet rs = null;
		//1.取出ajax 組件
		rs = utils.executeQuery("pan",onSearchGetSql());
		ret.SetSearchResult(rs);
	}
	
	//取出查詢語句
	protected String onSearchGetSql() throws Exception{
		StringBuilderEx sql =  new StringBuilderEx("select * from pan..pan_table where 1=1");
		if(!form.isNullOrEmpty("psysName")){
			sql.Append(" and sysname = '%1$s'",form.gpsv("psysname"));
		}
		if(!form.isNullOrEmpty("pdatasource")){
			sql.Append(" and datasource = '%1$s'",form.gpsv("pdatasource"));
		}
		if(!form.isNullOrEmpty("pdatabase")){
			sql.Append(" and database = '%1$s'",form.gpsv("pdatabase"));
		}
		if(!form.isNullOrEmpty("ptableName")){
			sql.Append(" and tableName like '%%%1$s%%'",form.gpsv("ptableName"));
		}
		if(!form.isNullOrEmpty("prem")){
			sql.Append(" and rem like N'%%%1$s%%'",form.gpsv("prem"));
		}
		sql.Append(" order by srt");
		return sql.toString();
	}
	
	//执行存储
	protected void doSave() throws Exception{
		StringBuilderEx sql = new StringBuilderEx();
		BaseDataEntities records = form.getrecords("grid1_records");
		for (int i=0;i<records.size();i++){
			BaseDataEntity record = (BaseDataEntity)records.get(i);
			CrudDataEntity crud = record.getCrudDataEntity("pan","pan..pan_table","sn","sysName,datasource,database,tablename,enabwf,rem,srt");
			crud.spsv("keyname", record.gpsv("keyname","'sn'"));
			crud.setSpportSimple(true);
			crud.setDebug(true);
			crud.setUtils(utils);
			crud.appendSaveCols();
			if(crud.isNullOrEmpty("datasource") 
				||crud.isNullOrEmpty("database")
				||crud.isNullOrEmpty("tablename")){
				continue;
			}
			
			
			crud.save();
			
			//新增表 
			rs = utils.executeQuery(crud.gpsv("datasource"), "select count(*) as cnt  from  "+crud.gpsv("database")+"..sysobjects where xtype='u' and name = '"+crud.gpsv("tablename")+"' ");
			if(rs.next() && rs.getInt("cnt") == 0){
				sql.Reset();
				sql.Append("\r\n CREATE TABLE %1$s..[%2$s]( ",crud.gpsv("database"),crud.gpsv("tablename"));
				sql.Append("\r\n [sn] [int] IDENTITY(1,1) NOT NULL ");
				sql.Append("\r\n CONSTRAINT [PK_%1$s] PRIMARY KEY CLUSTERED ",crud.gpsv("tablename"));
				sql.Append("\r\n ( ");
				sql.Append("\r\n [sn] ASC ");
				sql.Append("\r\n )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY], ");				
				sql.Append("\r\n [prgName] [nvarchar](150) NOT NULL default(''), ");
				sql.Append("\r\n [crtDay] [datetime] NOT NULL default(getdate()), ");
				sql.Append("\r\n [crtName] [nvarchar](50) NOT NULL default(''), ");
				sql.Append("\r\n [crtIp] [nvarchar](20) NOT NULL default(''), ");
				sql.Append("\r\n [updDay] [datetime] NOT NULL default(getdate()), ");
				sql.Append("\r\n [updName] [nvarchar](50) NOT NULL default(''), ");
				sql.Append("\r\n [updIp] [nvarchar](20) NOT NULL default('')");
				sql.Append("\r\n ) ON [PRIMARY] ");
				out(sql.toString());
				utils.executeUpdate(crud.gpsv("datasource"), sql.toString());
			}
		}
	}
	
	protected boolean onDelete() throws Exception{
		super.onDelete();
		onSearch();
		return true ;
	}
	
	protected void doDelete() throws Exception{
		String sns[] = form.gpsvs("key");
		if(sns.length>0){
			StringBuilderEx sql = new StringBuilderEx();
			sql.Append(" delete from pan..pan_table where sn in(%1$s )",StringHelper.arrayToString(sns,"'",","));
			sql.Append("\r\n delete from pan..pan_table_detail where tablesn in(%1$s) ",StringHelper.arrayToString(sns,"'",","));
			utils.executeUpdate("pan",sql.toString());
		}
	}
}
%>
<%@include file="/include/common/gridbackend.jsp"%>